package com.insta.playdroid;

import java.util.List;

import adapteurMediaStore.AdapteurAlbumMediaStore;
import adapteurMediaStore.IAdapteurMediaStore;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

import com.insta.adapterList.ImageAlbumAdapter;
import com.insta.playdroid.metier.domain.Album;
import com.insta.utils.HeadsetConnectionReceiver;
import com.insta.utils.SingletonPlayer;

public class AlbumActivity2 extends Activity {

	public static List<Album> albums;
	private IAdapteurMediaStore<Album> adapteurMediaStore;
	private HeadsetConnectionReceiver headsetReceiver;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/* creating and registering intent receiver */
		headsetReceiver = new HeadsetConnectionReceiver();
		registerReceiver(headsetReceiver, new IntentFilter(
				Intent.ACTION_HEADSET_PLUG));

		setContentView(R.layout.allalbums);

		// collecter les données
		adapteurMediaStore = new AdapteurAlbumMediaStore(getBaseContext());
		albums = adapteurMediaStore.collectAllValuesFromDB();

		GridView gridview = (GridView) findViewById(R.id.gridview);

		long largeurPixel = getResources().getDisplayMetrics().widthPixels;
		gridview.setAdapter(new ImageAlbumAdapter(this, albums, largeurPixel));

		final Intent albumSongs = new Intent(this, AlbumSongsActivity.class);
		gridview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				albumSongs.putExtra("album", position);
				albumSongs
						.putExtra("image", albums.get(position).getAlbumArt());

				anim(R.anim.rotate, v, albumSongs);
				// startActivity(albumSongs);
			}
		});

	}

	private void anim(int anim, View target1, final Intent intent) {

		Animation runAnimation = AnimationUtils.loadAnimation(this, anim);
		runAnimation.reset();
		target1.startAnimation(runAnimation);

		runAnimation.setAnimationListener(new Animation.AnimationListener() {

			@Override
			public void onAnimationEnd(Animation arg0) {
				startActivity(intent);
			}

			@Override
			public void onAnimationRepeat(Animation arg0) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onAnimationStart(Animation arg0) {
				// TODO Auto-generated method stub

			}
		});
	}

	/* unregistering intent receiver */
	@Override
	public void onDestroy() {
		super.onDestroy();
		unregisterReceiver(headsetReceiver);
		mPhoneListener = null;
	}


	/**
	 * Ecoute l'état des appels émis et sortants afin de gérer le player
	 */
	@SuppressWarnings("unused")
	private PhoneStateListener mPhoneListener = new PhoneStateListener()

	{
		public void onCallStateChanged(int state, String incomingNumber)

		{
			switch (state)

			{
			case TelephonyManager.CALL_STATE_RINGING:

			{
				if (SingletonPlayer.getInstancePlayer().isPlaying())
					SingletonPlayer.getInstancePlayer().pause();

				Log.v("CALL_STATE_RINGING", "Ringing");

			}

				break;

			case TelephonyManager.CALL_STATE_OFFHOOK:

			{
				if (SingletonPlayer.getInstancePlayer().isPlaying())
					SingletonPlayer.getInstancePlayer().pause();
				Log.v("CALL_STATE_OFFHOOK", "OffHook");
			}

				break;

			case TelephonyManager.CALL_STATE_IDLE:

			{
				if (!SingletonPlayer.getInstancePlayer().isPlaying())
					SingletonPlayer.getInstancePlayer().start();

				Log.v("CALL_STATE_IDLE", "Idle(No activity)");
			}
				break;

			default:

				Log.d("TAG", "Unknown phone state=" + state);

			}

		}

	};

}
